package com.cskaoyan.market.service.admin;/*
 *文件名: MarketOrderServiceImpl
 *创建者:wxin
 *创建时间:2024/5/8 22:03
 */


import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.db.domain.MarketOrderExample;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.vo.MarketOrderVo;
import com.cskaoyan.market.vo.MarketOrderVoColumns;
import com.cskaoyan.market.vo.TimeConUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
@Service
public class MarketStatOrderServiceImpl implements MarketStatOrderService {
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Override
    public MarketOrderVo getOrder() {

        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(new MarketOrderExample());

        Map<String,List<MarketOrder>> groupByTimeOrders=groupByTime(marketOrders);
        List<MarketOrderVoColumns> rows=new ArrayList<>();
        for (Map.Entry<String, List<MarketOrder>> stringListEntry : groupByTimeOrders.entrySet()) {
            MarketOrderVoColumns row=getRows(stringListEntry.getKey(),stringListEntry.getValue());
            rows.add(row);
        }
        MarketOrderVo data=new MarketOrderVo();
        data.setRows(rows);
        String[] s=new String[]{"day","orders","customers","amount","pcr"};
        data.setColumns(s);
        return data;
    }

    private MarketOrderVoColumns getRows(String key, List<MarketOrder> value) {
        MarketOrderVoColumns r1=new MarketOrderVoColumns();
        r1.setDay(key);
        List<BigDecimal> bigDecimalList=new ArrayList<>();
        Set<Integer> userIdList=new HashSet<>();
        for (MarketOrder marketOrder : value) {
            BigDecimal actualPrice = marketOrder.getActualPrice();
            Integer userId = marketOrder.getUserId();
            bigDecimalList.add(actualPrice);
            userIdList.add(userId);
        }
        int size1 = value.size();
        BigDecimal sum=bigDecimalList.stream().reduce(BigDecimal.ZERO,BigDecimal::add);
        int size = userIdList.size();
        BigDecimal value1 = BigDecimal.valueOf(size);
        BigDecimal divide = sum.divide(value1);
        r1.setOrders(size1);
        r1.setCustomers(size);
        r1.setAmount(sum);
        r1.setPcr(divide);
        return r1;
    }

    private Map<String, List<MarketOrder>> groupByTime(List<MarketOrder> marketOrders) {
        Map<String,List<MarketOrder>> listMap=new TreeMap<>();
        for (MarketOrder marketOrder : marketOrders) {
            LocalDateTime confirmTime = marketOrder.getConfirmTime();

            if(confirmTime!=null){
                String timeCon = TimeConUtil.TimeCon(confirmTime);
                List<MarketOrder> list=listMap.get(timeCon);
                if(list==null){
                    list=new ArrayList<>();
                }
                list.add(marketOrder);
                listMap.put(timeCon,list);
            }
        }

        return listMap;
    }
    }
